<template>
  <div>
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      stripe
      border
    >
      <el-table-column type="index" :index="indexMethod" width="50">
      </el-table-column>
      <el-table-column prop="name" label="名称" width="200"> </el-table-column>
      <el-table-column prop="type" label="类型" width="150"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="goto(scope.$index, scope.row)">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="tableData.length > 0" class="block" style="margin-top: 15px">
      <el-pagination
        align="center"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="tableData.length"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  props: ["table", "map"],

  data() {
    return {
      tableData: [],
      currentPage: 1, // 当前页码
      total: 19, // 总条数
      pageSize: 5, // 每页的数据条数
      baseMap: null,
    };
  },

  methods: {
    handleTableData(data) {
      if (!data) {
        return [];
      }
      let tableData = [];
      data.forEach((v) => {
        tableData.push({
          name: v.get("name"),
          type: v.getGeometry().getType(),
          feature: v,
        });
      });

      return tableData;
    },
    goto(index, row) {
      let popup = this.baseMap.getOverlays().getArray()[0];
      popup.setPosition(null);

      let select = this.baseMap.getInteractions().getArray()[8];
      select.getFeatures().clear();
      select.getFeatures().push(row.feature);
      select.dispatchEvent({
        type: "select",
        selected: [row.feature],
        mapBrowserEvent: {
          coordinate: row.feature.getGeometry().getFirstCoordinate(),
        },
      });
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
    indexMethod(index) {
      let id = index + (this.currentPage - 1) * this.pageSize + 1;
      return id;
    },
  },

  watch: {
    table() {
      this.tableData = this.handleTableData(this.table);
      this.total = this.tableData.length;
    },
    map() {
      this.baseMap = this.map;
    },
  },
};
</script>

<style scoped>
</style>